<?php
class ActivityStreamsPlugin extends SpecialPage {
	function __construct() {
		parent::__construct( 'ActivityStreamsPlugin' );
		wfLoadExtensionMessages('ActivityStreamsPlugin');
	}
 
	function execute( $par ) {
		global $wgRequest, $wgOut, $wgScriptPath;
		
		$func = new ActivityStreamsPluginFunctions();
		
		if($par == 'stream')
		{
		    $func->httpHeaders(); // Wiki Ausgabe deaktivieren
		    print $func->outHeader();
		    
		    $dbr = wfGetDB( DB_SLAVE );
		    
		    $res = $dbr->select(
		                        'recentchanges',
		                        '*',
		                        '',
		                        '',
		                        array(
		                              'ORDER BY' => 'rc_timestamp DESC',
		                              'LIMIT' => 10
		                             )
		                       );
		    // Einträge vorhanden
		    if($dbr->numRows($res) != '0')
		    {
		        for($i = 0; $i < $dbr->numRows($res); $i++)
		        {
		            $opt = array();
		            $obj = $dbr->fetchObject($res);
		            
		           //print_r($obj);
		            
		            if($obj->rc_new == '1')
		            {
		                $opt['verb'] = 'http://activitystrea.ms/schema/1.0/post';
		                $opt['summary'] = $func->xmlEncode($obj->rc_user_text.' '.wfMsg('activitystreamsplugin-newatricle').' '.$obj->rc_title);
		            }
		            else
		            {
		                $opt['verb'] = 'http://activitystrea.ms/schema/1.0/update';
		                $opt['summary'] = $func->xmlEncode($obj->rc_user_text.' '.wfMsg('activitystreamsplugin-updateatricle').' '.$obj->rc_title);
		            }
		            $opt['title'] = $func->xmlEncode($obj->rc_title);
		            $opt['updated'] = $obj->rc_timestamp;
		            $opt['author'] = $func->xmlEncode($obj->rc_user_text);
		            $opt['authorUserId'] = $func->xmlEncode($obj->rc_user);
		            $opt['authorURI'] = $func->xmlEncode(($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$wgScriptPath.'/index.php?title=User:'.$obj->rc_user_text);
		            $opt['comment'] = $func->xmlEncode($obj->rc_comment);
		            $opt['object-type'] = array();
		            
		            if($obj->rc_log_type == 'upload')
		            {
		                $opt['itemRcURI'] = ($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$wgScriptPath.'/index.php?title=File:'.$opt['title'];
		                $opt['itemURI'] = ($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$wgScriptPath.'/index.php?title=File:'.$opt['title'];
		                $opt['object-type'][] = 'http://www-user.tu-chemnitz.de/~schrae/activitystreams/wikipage';
		                $opt['object-type'][] = 'http://activitystrea.ms/schema/1.0/file';
		            }
		            else
		            {
		                $opt['itemRcURI'] = ($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$wgScriptPath.'/index.php?title='.$opt['title'].'&amp;diff='.$obj->rc_this_oldid.'&amp;oldid=prev';
		                $opt['itemURI'] = ($_SERVER['HTTPS'] ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$wgScriptPath.'/index.php?title='.$opt['title'];
		                $opt['object-type'][] = 'http://www-user.tu-chemnitz.de/~schrae/activitystreams/wikipage';
		                $opt['object-type'][] = 'http://activitystrea.ms/schema/1.0/article';
		            }
		            
		            print $func->outItem($opt);
		        }
		    }
		    else
		    {
		        print '<!-- nothing done in MediaWiki right now -->';
		    }
		    
		    print $func->outFooter();
		}
		else
		{
		    $wgOut->setPagetitle("ActivityStrea.ms Export");
		    //$this->setHeaders(); -- schrae: benötigt??
		    $wgOut->addWikiText(wfMsg('activitystreamsplugin-desc'));
		    $wgOut->addWikiText(wfMsg('activitystreamsplugin-linktext').' '.$_SERVER['SCRIPT_URI'].'/stream');
		    
		    //print_r($GLOBALS);
		}
	}
}
?>
